#数据表ORM
from sqlalchemy import Column, Integer, String
from database import Base
from sqlalchemy import Column, Integer, String, Text,DateTime, ForeignKey
from sqlalchemy.orm import relationship
from datetime import datetime
from database import engine
Base.metadata.create_all(bind=engine)
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    username = Column(String(50), unique=True, index=True, nullable=False)
    password = Column(String(200), nullable=False)
    role = Column(String(20), default="student")  # student / teacher / admin

class Course(Base):
    __tablename__ = "courses"
    id = Column(Integer, primary_key=True, index=True)
    name = Column(String(100), unique=True, nullable=False)

    teacher_id = Column(Integer, ForeignKey("users.id"))
    teacher = relationship("User")

class Problem(Base):
    __tablename__ = "problems"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(200), nullable=False)  # 题目标题
    description = Column(Text, nullable=False)   # 题目描述
    type = Column(String(20), nullable=False)    # 题目类型: choice / blank / code / short
    answer = Column(Text, nullable=True)         # 标准答案（选择/填空/简答）
    test_cases = Column(Text, nullable=True)     # 测试用例(JSON字符串，编程题用)

class Homework(Base):
    __tablename__ = "homeworks"

    id = Column(Integer, primary_key=True, index=True)
    title = Column(String(100), nullable=False)
    description = Column(Text, nullable=True)
    problems = Column(Text, nullable=False)  # 存储题目ID列表，JSON字符串
    deadline = Column(DateTime, nullable=True)
    teacher_id = Column(Integer, ForeignKey("users.id"))  # 哪个老师布置的

    teacher = relationship("User")
    submissions = relationship("Submission", back_populates="homework")


class Submission(Base):
    __tablename__ = "submissions"

    id = Column(Integer, primary_key=True, index=True)
    homework_id = Column(Integer, ForeignKey("homeworks.id"))
    problem_id = Column(Integer, ForeignKey("problems.id"))
    student_id = Column(Integer, ForeignKey("users.id"))

    code = Column(Text, nullable=False)  # 学生提交的代码
    status = Column(String(20), default="Pending")  # 结果: Pending/AC/WA/RE/TLE
    submit_time = Column(DateTime, default=datetime.utcnow)

    # 方便 ORM 查询
    homework = relationship("Homework", back_populates="submissions")
    problem = relationship("Problem")
    student = relationship("User")